package com.zadetk.helpyou.activities;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.goyourfly.multi_picture.ImageLoader;
import com.goyourfly.multi_picture.MultiPictureView;
import com.zadetk.helpyou.R;
import com.zadetk.helpyou.adapter.MyAdapter;
import com.zadetk.helpyou.base.AbsActivity;
import com.zadetk.helpyou.bean.JobinsertBean;
import com.zadetk.helpyou.net.BaseObserver;
import com.zadetk.helpyou.net.BaseResponse;
import com.zadetk.helpyou.net.NetTool;
import com.zadetk.helpyou.other.UserInfoManger;
import com.zadetk.helpyou.utils.LogUtil;
import com.zadetk.helpyou.utils.ToastUtil;
import com.zadetk.helpyou.view.NoScrollView;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import me.nereo.multi_image_selector.MultiImageSelector;

/**
 * 6-1发布岗位页面
 */
public class PublishJobActivity extends AbsActivity implements View.OnClickListener {

    private static final String TAG = "";
    private NoScrollView gvCategory;
    private EditText etTitle;
    private EditText etContent;
    private MultiPictureView multipleImage;
    private EditText etLow;
    private EditText etHigh;
    private Switch sw;
    private TextView tvBtnSubmit;
    private ArrayList<String> list = new ArrayList<>();
    private File upFile;


    private void initViews() {
        gvCategory = (NoScrollView) findViewById(R.id.gv_category);
        etTitle = (EditText) findViewById(R.id.et_title);
        etContent = (EditText) findViewById(R.id.et_content);
        multipleImage = (MultiPictureView) findViewById(R.id.multiple_image);
        etLow = (EditText) findViewById(R.id.et_low);
        etHigh = (EditText) findViewById(R.id.et_high);
        sw = (Switch) findViewById(R.id.sw);
        tvBtnSubmit = (TextView) findViewById(R.id.tv_btn_submit);
        setTopTitle("发布岗位");
    }

    @Override
    public int setView() {
        return R.layout.activity_publish_job;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        initViews();
        initLogic();
        requestData();
    }

    private void requestData() {


    }

    private void initLogic() {
        MultiPictureView.setImageLoader(new ImageLoader() {
            @Override
            public void loadImage(ImageView image, Uri uri) {
                Glide.with(PublishJobActivity.this)
                        .load(uri)
                        .placeholder(R.drawable.placeholder)
                        .error(R.drawable.placeholder)
                        .into(image);
            }
        });
        multipleImage.setAddClickCallback(new MultiPictureView.AddClickCallback() {
            @Override
            public void onAddClick(View view) {
                performCodeWithPermission("需要相机权限", new PermissionCallback() {
                    @Override
                    public void hasPermission() {
                        MultiImageSelector.create().showCamera(true).multi().count(9).start(PublishJobActivity.this, 200);
                    }

                    @Override
                    public void noPermission() {

                    }
                }, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA});
            }
        });

        list.add("");
        list.add("");
        list.add("");
        list.add("");
        list.add("");
        list.add("");
        list.add("");
        gvCategory.setAdapter(new MyAdapter<String>(list, R.layout.tag_checkbox) {
            @Override
            public void bindView(ViewHolder holder, String obj) {

            }
        });
        tvBtnSubmit.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_btn_submit:
                ToastUtil.showToast("提交请求！");
                getJobinsert();
                break;

        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == 200) {
            ArrayList<String> list = data.getStringArrayListExtra(MultiImageSelector.EXTRA_RESULT);
            LogUtil.d(list.get(0));
            ToastUtil.showToast(list.get(0));
            for (String path : list) {
                File file = new File(path);
                upFile = file;
                Uri uri = Uri.fromFile(file);
                multipleImage.addItem(uri);
            }
        }
    }

    // FIXME: 2018/7/19 0019 图片上传和分类还有些问题
    private void getJobinsert() {


        Map<String, Object> param = NetTool.newParams();
        param.put("user_id", UserInfoManger.userInfo.getUid());
        param.put("token", UserInfoManger.userInfo.getUserToken());
        param.put("service", 2);
        param.put("title", etTitle.getText().toString());
        param.put("content", etContent.getText().toString());
        param.put("image", upFile);
        param.put("high", etLow.getText());
        param.put("low", etHigh.getText());
        param.put("is", 2);
        NetTool.getApi().getJobinsert(param)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<List<JobinsertBean>>>() {

                    @Override
                    public void onData(BaseResponse<List<JobinsertBean>> value) {
                        if (value.getCode() == 0) {
                            Log.i(TAG, "=================" + "有数据");
                            ToastUtil.showToast("提交成功！");
                        } else {
                            LogUtil.e(value.error_code + " error code");
                            Log.i(TAG, "=================" + "无数据");
                        }
                    }
                });
    }


}
